package com.xzx.study.provider;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.xzx.study.R;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

public class ContentProviderActivity extends AppCompatActivity implements View.OnClickListener {

    private Button provider_insert;
    private Button provider_delete;
    private Button provider_update;
    private Button provider_query;
    private EditText provider_phone;
    private Button provider_choice;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_provider);

        provider_insert = findViewById(R.id.provider_insert);
        provider_delete = findViewById(R.id.provider_delete);
        provider_update = findViewById(R.id.provider_update);
        provider_query = findViewById(R.id.provider_query);

        provider_phone = findViewById(R.id.provider_phone);
        provider_choice = findViewById(R.id.provider_choice);


        provider_insert.setOnClickListener(this);
        provider_delete.setOnClickListener(this);
        provider_update.setOnClickListener(this);
        provider_query.setOnClickListener(this);

        provider_choice.setOnClickListener(this);

        if (AndPermission.hasAlwaysDeniedPermission(this, Permission.READ_CONTACTS)) {
            AndPermission.with(this).runtime().permission(Permission.READ_CONTACTS).start();
        }
    }

    @Override
    public void onClick(View v) {
        ContentResolver contentResolver = getContentResolver();
        if (v == provider_insert) {
            Uri uri = Uri.parse("content://com.xzx.study.provider.studentprovider/student");
            ContentValues values = new ContentValues();
            values.put("name", "李四");
            contentResolver.insert(uri, values);
        } else if (v == provider_delete) {
            Uri uri = Uri.parse("content://com.xzx.study.provider.studentprovider/student/2");
            contentResolver.delete(uri, null, null);
        } else if (v == provider_update) {
            Uri uri = Uri.parse("content://com.xzx.study.provider.studentprovider/student/2");
            ContentValues values = new ContentValues();
            values.put("name", "王五");
            contentResolver.update(uri, values, null, null);
        } else if (v == provider_query) {
            Uri uri = Uri.parse("content://com.xzx.study.provider.studentprovider/student");
            Cursor cursor = contentResolver.query(uri, new String[]{"_id", "name"}, null, null, null);
            if (cursor!= null) {
                StringBuilder builder = new StringBuilder();
                while (cursor.moveToNext()) {
                    int id = cursor.getInt(0);
                    String name = cursor.getString(1);
                    builder.append(id + ":" + name + "========");
                }
                cursor.close();
                Toast.makeText(this, builder.toString(), Toast.LENGTH_SHORT).show();
            }
        } else if (v == provider_choice) {
            startActivityForResult(new Intent(this, SelectContactActivity.class), 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            String number = data.getStringExtra("number");
            provider_phone.setText(number);
        }
    }
}
